﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Text;
using Dotnet.TopApi.Response;

namespace Dotnet.TopApi.Request.WesafeDataAPIService
{
    //public interface ITopRequest<T> where T : TopResponse
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="R">请求返回的类型</typeparam>
    /// <typeparam name="E">请求的实体类</typeparam>
    /// <typeparam name="P">请求的参数类</typeparam>
    public class WesafeDataAPIServiceRequestBase<R,E,P> : ITopRequest<R> where R : WesafeDataAPIServiceResponseBase where P:new()
    {
        private List<E> _entitys = new List<E>();
        private HanderConfig _handerConfig = new HanderConfig();
        private P _paramsConfig = new P();

        public List<E> Entitys
        {
            get { return _entitys; }
            set { _entitys = value; }
        }
        public HanderConfig HanderConfig
        {
            get { return _handerConfig; }
            set { _handerConfig = value; }
        }
        public P ParamsConfig
        {
            get { return _paramsConfig; }
            set { _paramsConfig = value; }  
        }


        public virtual string GetApiName()
        {
            return "WesafeDataAPIServiceRequest";
        }

        public virtual IDictionary<string, string> GetParameters()
        {
            TopDictionary parameters = new TopDictionary();
            parameters.AddHeaders("method", "POST");
            parameters.AddHeaders("ContentType", "application/json");

            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(new { entitys = _entitys, handerConfig = _handerConfig, paramsConfig = _paramsConfig }, timeConverter);
            parameters.AddBobyContent(json);
            return parameters;
        }

        public virtual void Validate()
        {
            //throw new NotImplementedException();
        }


        //string authorization = "";
        //parameters?.TryGetValue("Header:Authorization", out authorization);
        //    if (authorization != "")
        //    {
        //        req.Headers.Add("Authorization", authorization);
        //    }
}
}
